package branch_and_price;

import gurobi.GRB;
import gurobi.GRBEnv;
import gurobi.GRBException;
import gurobi.GRBLinExpr;
import gurobi.GRBModel;
import gurobi.GRBVar;

public class Gurobi {
    public static void main(String[] args) {
        int m = 10;
        int n = 40;
        try {

            GRBEnv env = new GRBEnv(true);
            env.start();
            GRBModel model = new GRBModel(env);
            // model.set("OutputFlag", "0");

            GRBVar[][] x = new GRBVar[m][];
            for (int i = 0; i < m; i++) {
                x[i] = model.addVars(n, GRB.BINARY);
            }

            GRBLinExpr expr = new GRBLinExpr();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    expr.addTerm(Dataset.profit[i][j], x[i][j]);
                }
            }
            model.setObjective(expr, GRB.MAXIMIZE);

            for (int j = 0; j < n; j++) {
                expr = new GRBLinExpr();
                for (int i = 0; i < m; i++) {
                    expr.addTerm(1, x[i][j]);
                }
                model.addConstr(expr, GRB.EQUAL, 1, "c0");
            }

            for (int i = 0; i < m; i++) {
                expr = new GRBLinExpr();
                for (int j = 0; j < n; j++) {
                    expr.addTerm(Dataset.cost[i][j], x[i][j]);
                }
                model.addConstr(expr, GRB.LESS_EQUAL, Dataset.capacity[i], "c0");
            }

            model.optimize();

            System.out.println(model.get(GRB.DoubleAttr.ObjVal));

        } catch (GRBException e) {
            System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage());
        }
    }
}
